***reading in zipped men data, unziiping and retaining only the files I need
***https://u.osu.edu/odden.2/2013/09/26/use-stata-to-unzip-a-bunch-of-demographic-and-health-survey-files-and-put-them-where-i-want-them/

clear all
set more off
capture log close
local sourcedir "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
local unzipdir "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/TEMP"
cap noi mkdir "`unzipdir'"

/*
    First, let's unzip all the zip files we find
*/
local fls : dir "`sourcedir'" files "*.ZIP"
cd "`unzipdir'"
foreach f of local fls {
        di "Working on `f'"
        unzipfile "`sourcedir'/`f'", replace
}
/* Let's:
    1. Make the filename lowercase,
    2. strip the last two characters of the basename,
    3. infer the type of survey (individual, etc.) from the 3rd-4th chars,
    4. construct a new filename we like, and
    5. move the file where we want it
*/
local fls : dir "`unzipdir'" files "*.DTA"
foreach f of local fls {
        local lname = lower("`f'")
        local stem = substr("`lname'", 1, 6)
        local surv_type = substr("`lname'",3,2)
        local fname = "`stem'.DTA"
        copy "`unzipdir'/`f'" "`sourcedir'/`fname'", replace
}
* Let's clean up the files that were unzipped
local fls : dir "`unzipdir'" files "*"
foreach f of local fls {
    erase "`unzipdir'/`f'"
}
* And clean up the unzipped files themselves
local fls : dir "`sourcedir'" files "*.ZIP"
foreach f of local fls {
    erase "`sourcedir'/`f'"
}
*********reading in zipped women data, unziiping and retaining only the files I need
*
clear all
set more off
capture log close
local sourcedir "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN"
local unzipdir "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/TEMP"
cap noi mkdir "`unzipdir'"

/*
    First, let's unzip all the zip files we find
*/
local fls : dir "`sourcedir'" files "*.ZIP"
cd "`unzipdir'"
foreach f of local fls {
        di "Working on `f'"
        unzipfile "`sourcedir'/`f'", replace
}
/* Let's:
    1. Make the filename lowercase,
    2. strip the last two characters of the basename,
    3. infer the type of survey (individual, etc.) from the 3rd-4th chars,
    4. construct a new filename we like, and
    5. move the file where we want it
*/
local fls : dir "`unzipdir'" files "*.DTA"
foreach f of local fls {
        local lname = lower("`f'")
        local stem = substr("`lname'", 1, 6)
        local surv_type = substr("`lname'",3,2)
        local fname = "`stem'.DTA"
        copy "`unzipdir'/`f'" "`sourcedir'/`fname'", replace
}
* Let's clean up the files that were unzipped
local fls : dir "`unzipdir'" files "*"
foreach f of local fls {
    erase "`unzipdir'/`f'"
}
* And clean up the unzipped files themselves
local fls : dir "`sourcedir'" files "*.ZIP"
foreach f of local fls {
    erase "`sourcedir'/`f'"
}
*******now reading them in to stata and appending 
****first men
clear all
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
set maxvar 31000
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
! ls *.DTA >mendata.txt

clear all

file open myfile using "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN/mendatalit.txt", read
file read myfile line
use `line'



save master_data_men, replace

file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	append using `line', keep(mv005 mv155 mv133  mv190   mv000 mv001 mv002 mv003 mv024 mv025  mv021 mcaseid mv013 ///
mv007 mv106 mv005  mv191 mv010 mv012  mv136 mv157 mv201 mv206 mv207 mv212 mv716 mv731 )
	file read myfile line
}
file close myfile
gen weight=mv005/1000000
svyset [pweight=weight], psu(mv021)

egen manid = group(mv000 mv001 mv002 mv003 mv024 mv025)


egen cluster = group(mv000 mv024 mv025 mv001)
egen clustername=concat(mv000 cluster)

keep  weight mv005 mv155 mv133  mv190  clustername  mv000 mv001 mv002 mv003 mv024 mv025  mcaseid mv013 ///
mv007 mv106 mv005  mv191 mv010 mv012  mv136 mv157 mv201 mv206 mv207 mv212 mv716 mv731  



foreach var of varlist mcaseid-mv731 {
   	local newname = substr("`var'", 2, .)
   	rename `var' `newname'
}
gen Female=0
save master_data_men, replace

****now women -
clear all
set maxvar 31000
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN"
! ls *.DTA >womendatalit.txt


file open myfile using "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN/womendatalit.txt", read
file read myfile line
use `line'



save master_data_women, replace

file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	append using `line', keep  (v155 v133 v116  v190   v000 v001 v002 v024 v003 v025 caseid v013 ///
v007 v106 v133 v005  v191 v010 v012 v119-v130 v136 v155 v157 v201 v206 v207 v212 v439 v438 v453 v445 v457 v602 v605 v021 v024 v613 v628 v627  v025 caseid v013 v113 v115 v457 v465)

	file read myfile line
}
file close myfile
gen weight=v005/1000000
svyset [pweight=weight], psu(v021)
*drop if hw1>60



gen open_defac=.
replace open_defac=1 if v116==31
replace open_defac=0 if v116~=31 & v116~=.

egen momid = group(v000 v001 v002 v003 v024 v025)
gen momHAZ= v440/100 if v440<=600
gen momWHA= v444a/100 if v444a<=600


egen open_defac_by_psu=mean(open_defac), by(v021)

gen open_defac_by_psusq=(open_defac_by_psu)^(2)


egen cluster = group(v000 v024 v025 v001)

egen open_defac_by_psu1=mean(open_defac), by(cluster)

gen open_defac_by_psusq1=(open_defac_by_psu)^(2)

egen clustername=concat(v000 cluster)
keep  v155 v133 v116  v190 momHAZ momWHA  clustername  open_defac_by_psu1 open_defac_by_psusq1 open_defac_by_psu open_defac_by_psusq v000 v001 v002 v003  v025 caseid v013 ///
v007 v106 v133 v005  v191 v010 v012 v119-v130 v136 v155 v157 v201 v206 v207 v212 v439 v438 v453 v445 v457 v602 v605 v021 v024 v613 v628 v627  v025 caseid v013 v113 v115 v457 v465
gen Female=1

save master_data_women, replace
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
append using master_data_men
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS"
save master_data_women_men, replace
split v000, p("4" "5" "6" "7" "8")
rename v0001 ccode
tab ccode
save master_data_women_men, replace
clear all
use master_data_women_men
sort ccode
merge m:1 ccode using country_codes_DHS
tab _merge
save master_data_women_men, replace
clear all
use master_data_women_men
gen notmatced=_merge
drop _merge
sort countryname
merge m:1 countryname using lang_policy
tab _merge
save master_data_women_men, replace
 
















